<?php

class ClientManageController extends AdminController {

    public function actionView() {
        $page = isset($_GET['p']) ? $_GET['p'] : 1;
        $dataPerpage = 5;
        if (isset($_GET['Keyword']) || isset($_GET['Province'])) {
            $Keyword = $_GET['Keyword'];
            $Province = $_GET['Province'];
            $customerList = Client::getCustomerListByKeyword($Keyword, $Province, $page, $dataPerpage);
        } else {
            $customerList = Client::getCustomerList($page, $dataPerpage);
        }
        $this->render('customer', array('data' => $customerList, 'page' => $page, 'dataPerpage' => $dataPerpage));
    }

    public function actionForm() {
        $ClientID = isset($_GET['ClientID']) ? $_GET['ClientID'] : '';
        $dataClient = isset($_GET['ClientID']) ? Client::getDetailCustomer($ClientID) : array();
        $this->render('form', array('dataClient' => $dataClient)
        );
    }

    public function actionSave() {
        if (isset($_POST)) {
            $ClientID = $_POST['ClientID'];
            $ContactName = $_POST['ContactName'];
            $CompanyName = $_POST['CompanyName'];
            $Address = $_POST['Address'];
            $ProvinceID = $_POST['Province'];
            $ZipCode = $_POST['ZipCode'];
            $Email = isset($_POST['Email'])?$_POST['Email']:'';
            $Phone = $_POST['Phone'];
            $Fax = $_POST['Fax'];
            $Mobile = $_POST['Mobile'];
            
             if ($ClientID == '') {
                Client::insertCustomer($ContactName, $CompanyName, $Address, $ProvinceID, $ZipCode, $Email, $Phone, $Fax, $Mobile, 'en', 'I');
            } else {
                Client::updateCustomer($ClientID, $ContactName, $CompanyName, $Address, $ProvinceID, $ZipCode, $Phone, $Fax, $Mobile);
            }
        }
        $this->redirect("index.php?r=ClientManage/View");
    }

    public function actionCheckEmail() {
        if (isset($_POST['Email'])) {
            $email = $_POST['Email'];
            echo Client::CheckEmail($email);
            return;
        }
    }
    
    public function actionEditStatus(){
         if (isset($_POST)) {
            $ClientID = $_POST['ClientID'];
            $Status = $_POST['Status'];
            echo Client::editStatusCustomer($ClientID, $Status);
        }
    }
    
    public function actionDelete() {
        if (isset($_POST)) {
            $ClientID = $_POST['ClientID'];
            Client::deleteCustomer($ClientID);
        }
        $this->redirect("index.php?r=ClientManage/View&p={$_POST['p']}");
    }

}

?>
